﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class OutboundValidationDA : IOutboundValidationDA
    {
        private string CONSIGNEE_ID = "@CONSIGNEE_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string IS_DEFAULT = "@IS_DEFAULT";
        private string MINIMUM_SHELF_LIFE = "@MINIMUM_SHELF_LIFE";
        private string OWNER_ID = "@OWNER_ID";
        private string SKU_ID = "@SKU_ID";
        private string SQL_CHECK_OUTBOUND_VALIDATION_ID_UNIQUE = " SELECT COUNT(1) FROM @_@OUTBOUND_VALIDATION WHERE VALIDATION_ID = @VALIDATION_ID ";
        private string SQL_DELETE_OUTBOUND_VALIDATION = " DELETE FROM @_@OUTBOUND_VALIDATION WHERE VALIDATION_ID = @VALIDATION_ID ";
        private string SQL_INSERT_OUTBOUND_VALIDATION = " INSERT INTO @_@OUTBOUND_VALIDATION ( WH_ID, VALIDATION_ID, DESCR, OWNER_ID, SKU_ID, CONSIGNEE_ID, TYPE, IS_ACTIVE, SYSTEM_CREATED,MINIMUM_SHELF_LIFE, IS_DEFAULT, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @VALIDATION_ID, @DESCR, @OWNER_ID, @SKU_ID, @CONSIGNEE_ID, @TYPE, @IS_ACTIVE,@SYSTEM_CREATED, @MINIMUM_SHELF_LIFE, @IS_DEFAULT, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_OUTBOUND_VALIDATION = " SELECT  ROWID, WH_ID, VALIDATION_ID, DESCR, OWNER_ID, SKU_ID, CONSIGNEE_ID, TYPE, IS_ACTIVE,SYSTEM_CREATED, MINIMUM_SHELF_LIFE, IS_DEFAULT, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@OUTBOUND_VALIDATION ";
        private string SQL_UPDATE_OUTBOUND_VALIDATION = " UPDATE @_@OUTBOUND_VALIDATION SET WH_ID = @WH_ID, DESCR = @DESCR, OWNER_ID = @OWNER_ID, SKU_ID = @SKU_ID, CONSIGNEE_ID = @CONSIGNEE_ID, TYPE = @TYPE, IS_ACTIVE = @IS_ACTIVE, MINIMUM_SHELF_LIFE = @MINIMUM_SHELF_LIFE, IS_DEFAULT = @IS_DEFAULT, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE(),SYSTEM_CREATED=@SYSTEM_CREATED WHERE VALIDATION_ID = @VALIDATION_ID  ";
        private string SYSTEM_CREATED = "@SYSTEM_CREATED";
        private string TYPE = "@TYPE";
        private string UPDATED_BY = "@UPDATED_BY";
        private string VALIDATION_ID = "@VALIDATION_ID";
        private string WH_ID = "@WH_ID";

        public bool CheckOutboundValidationIDUnique(string outboundValidationID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundValidationID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_OUTBOUND_VALIDATION_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckOutboundValidationIDUnique(DataBase dataBase, DbTransaction tran, string outboundValidationID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundValidationID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_OUTBOUND_VALIDATION_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteOutboundValidation(List<string> outboundValidationID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < outboundValidationID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundValidationID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_OUTBOUND_VALIDATION.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteOutboundValidation(DataBase dataBase, DbTransaction tran, List<string> outboundValidationID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < outboundValidationID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundValidationID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_OUTBOUND_VALIDATION.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllOutboundValidation(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllOutboundValidation(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public OutboundValidationInfo GetOutboundValidationByID(string outboundvalidationID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION.Replace("@_@", whLoginID) + " WHERE VALIDATION_ID = @VALIDATION_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundvalidationID) };
            OutboundValidationInfo outboundValidationInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    outboundValidationInfo = new OutboundValidationInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["VALIDATION_ID"].ToString(), reader["DESCR"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["CONSIGNEE_ID"].ToString(), reader["TYPE"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (reader["MINIMUM_SHELF_LIFE"].ToString() != "") ? int.Parse(reader["MINIMUM_SHELF_LIFE"].ToString()) : 0, reader["IS_DEFAULT"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return outboundValidationInfo;
        }

        public OutboundValidationInfo GetOutboundValidationByID(DataBase dataBase, DbTransaction tran, string outboundvalidationID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION.Replace("@_@", whLoginID) + " WHERE VALIDATION_ID = @VALIDATION_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VALIDATION_ID", outboundvalidationID) };
            OutboundValidationInfo outboundValidationInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                outboundValidationInfo = new OutboundValidationInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["VALIDATION_ID"].ToString(), reader["DESCR"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["CONSIGNEE_ID"].ToString(), reader["TYPE"].ToString(), reader["IS_ACTIVE"].ToString(), reader["SYSTEM_CREATED"].ToString(), (reader["MINIMUM_SHELF_LIFE"].ToString() != "") ? int.Parse(reader["MINIMUM_SHELF_LIFE"].ToString()) : 0, reader["IS_DEFAULT"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return outboundValidationInfo;
        }

        public DataSet GetOutboundValidationByQueryList(List<string> sqlWhere, OutboundValidationQueryEntity outboundvalidationQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!outboundvalidationQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, outboundvalidationQuery.CurrentPage, outboundvalidationQuery.PageSize, outboundvalidationQuery.SortField, outboundvalidationQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetOutboundValidationByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, OutboundValidationQueryEntity outboundvalidationQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_OUTBOUND_VALIDATION.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, outboundvalidationQuery.CurrentPage, outboundvalidationQuery.PageSize, outboundvalidationQuery.SortField, outboundvalidationQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertOutboundValidation(OutboundValidationInfo outboundValidationInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                outboundValidationInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_OutboundValidation_Parameters(outboundValidationInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_OUTBOUND_VALIDATION.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertOutboundValidation(DataBase dataBase, DbTransaction tran, OutboundValidationInfo outboundValidationInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                outboundValidationInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_OutboundValidation_Parameters(outboundValidationInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_OUTBOUND_VALIDATION.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_OutboundValidation_Parameters(OutboundValidationInfo outboundValidationInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.VALIDATION_ID, SqlDbType.VarChar, 20), new SqlParameter(this.DESCR, SqlDbType.VarChar, 100), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SKU_ID, SqlDbType.VarChar, 50), new SqlParameter(this.CONSIGNEE_ID, SqlDbType.VarChar, 20), new SqlParameter(this.TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.MINIMUM_SHELF_LIFE, SqlDbType.Int, 4), new SqlParameter(this.IS_DEFAULT, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.SYSTEM_CREATED, SqlDbType.VarChar, 1) };
            if (!string.IsNullOrEmpty(outboundValidationInfo.WhID))
            {
                paramArray[0].Value = outboundValidationInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = outboundValidationInfo.ValidationID;
            paramArray[2].Value = outboundValidationInfo.Descr;
            paramArray[3].Value = outboundValidationInfo.OwnerID;
            paramArray[4].Value = outboundValidationInfo.SkuID;
            paramArray[5].Value = outboundValidationInfo.ConsigneeID;
            paramArray[6].Value = outboundValidationInfo.Type;
            paramArray[7].Value = outboundValidationInfo.IsActive;
            paramArray[8].Value = outboundValidationInfo.MinimumShelfLife;
            paramArray[9].Value = outboundValidationInfo.IsDefault;
            paramArray[10].Value = outboundValidationInfo.CreatedBy;
            paramArray[11].Value = outboundValidationInfo.UpdatedBy;
            paramArray[12].Value = outboundValidationInfo.SystemCreated;
            return paramArray;
        }

        public int UpdateOutboundValidation(OutboundValidationInfo outboundValidationInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                outboundValidationInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_OutboundValidation_Parameters(outboundValidationInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_OUTBOUND_VALIDATION.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateOutboundValidation(DataBase dataBase, DbTransaction tran, OutboundValidationInfo outboundValidationInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                outboundValidationInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_OutboundValidation_Parameters(outboundValidationInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_OUTBOUND_VALIDATION.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

